<?php

class Nuo_Form extends Zend_Form
{
	/**
	 * The title of the form
	 *
	 * @var string
	 */
	protected $_title;

	/**
	 * An array of messages (non-warning)
	 *
	 * @var array
	 */
	protected $_messages;

	/**
	 * Default display group class
	 *
	 * @var string
	 */
	 protected $_defaultDisplayGroupClass = 'Nuo_Form_DisplayGroup';

	/**
	 * Constructor
	 *
	 * Registers form view helper as decorator
	 *
	 * @param mixed $options
	 * @return void
	 */
	public function __construct($options = null)
	{
		if (is_array($options))
		{
			$this -> setOptions($options);
		}
		elseif ($options instanceof Zend_Config)
		{
			$this -> setConfig($options);
		}

		$this -> addPrefixPath('Zend_Form_Element', LIBRARY_PATH . '/Zend/Form/Element', 'Element');
		$this -> addPrefixPath('Zend_Form_Decorator', LIBRARY_PATH . '/Zend/Form/Decorator', 'Decorator');
		$this -> addPrefixPath('Nuo_Form_Element', LIBRARY_PATH . '/Nuo/Form/Element', 'Element');
		$this -> addPrefixPath('Nuo_Form_Decorator', LIBRARY_PATH . '/Nuo/Form/Decorator', 'Decorator');

		// Extensions...
		$this -> init();

		$this -> loadDefaultDecorators();
	}

	public function getDefaultDecorators()
	{

		return array(
				
				'ViewHelper',
				array(
						'decorator' => 'HtmlTag3',
						'options' => array(
								'class' => 'form-ele'
						)
				),
				array('decorator'=>'errors'),
				array(
						'decorator' => 'Description',
						'options' => array(
								'tag' => 'p',
								'class' => 'form-ele-description',
								'placement' => 'PREPEND'
						)
				),
				array(
						'decorator' => 'HtmlTag2',
						'options' => array(
								'class' => 'form-ele-stage'
						)
				),
				array(
						'decorator' => 'Label',
						'options' => array('tag' => 'div','tagOptions'=>array('class'=>'form-ele-label'))
				),
				array(
						'decorator' => 'HtmlTag',
						'options' => array('class' => 'form-ele-wrapper')
				),
		);
	}

	/**
	 * Load the default decorators
	 *
	 * @return Zend_Form
	 */
	public function loadDefaultDecorators()
	{
		if ($this -> loadDefaultDecoratorsIsDisabled())
		{
			return $this;
		}

		$decorators = $this -> getDecorators();
		if (empty($decorators))
		{
			$this -> addDecorator('FormElements') -> addDecorator('HtmlTag', array(
					'tag' => 'div',
					'class' => 'form-elements'
			)) 
			-> addDecorator('FormMessages', array('placement' => 'PREPEND'))
			//-> addDecorator('FormErrors', array('placement' => 'PREPEND'))
			-> addDecorator('Description', array(
					'placement' => 'PREPEND',
					'class' => 'form-description'
			)) -> addDecorator('FormTitle', array(
					'placement' => 'PREPEND',
					'tag' => 'h3',
					'class'=>'form-title',
			))
			 -> addDecorator('FormWrapper', array('tag' => 'div','class'=>'form-stage')) 
			 -> addDecorator('FormContainer', array('tag' => 'div','class'=>'form-inner'))
			 -> addDecorator('Form');
		}
		return $this;
	}

	// Options

	/**
	 * Set form title
	 *
	 * @param string $title
	 * @return Engine_Form
	 */
	public function setTitle($title)
	{
		$this -> _title = $title;
		return $this;
	}

	/**
	 * Get current form title
	 *
	 * @return string
	 */
	public function getTitle()
	{
		return $this -> _title;
	}

	// Messaging

	/**
	 * Add a notice
	 *
	 * @param string $message
	 * @return Engine_Form
	 */
	public function addMessage($message)
	{
		$this -> _messages[] = $message;
		return $this;
	}

	/**
	 * Clear all notices
	 *
	 * @return Engine_Form
	 */
	public function clearMessages()
	{
		$this -> _messages = array();
		return $this;
	}

	/**
	 * Get all notices
	 *
	 * @return array
	 */
	public function getMessages()
	{
		return (array)$this -> _messages;
	}

	public function createElement($type, $name, $options = null)
	{
		if (is_string($type))
		{
			$options = (array)$options;
			if (!isset($options['decorators']))
			{
				$options['decorators'] = $this -> getDefaultDecorators();
			}
		}
		return parent::createElement($type, $name, $options);
	}

	public function addElement($element, $name = null, $options = null)
	{
		if (is_string($element))
		{
			$options = (array)$options;
			if (!isset($options['decorators']))
			{
				$options['decorators'] = $this -> getDefaultDecorators();
			}
		}
		return parent::addElement($element, $name, $options);
	}

}
